package com.library.evaluate.service;

import com.framework.Page;
import com.library.entity.Book;
import com.library.entity.Evaluate;
import com.library.entity.Staff;
import com.library.evaluate.dao.EvaluateDaoImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly=false)
public class EvaluateServiceImpl {
	
	@Resource
	private EvaluateDaoImpl evaluateDaoImpl;

	public void addEvaluate(Evaluate evaluate){
		this.evaluateDaoImpl.saveEvaluate(evaluate);
	}

    @Transactional(readOnly=true)
	public Evaluate getEvaluateById(Integer evaluateId){
		return this.evaluateDaoImpl.getEvaluateById(evaluateId);
	}

    @Transactional(readOnly=true)
	public List<Evaluate> getEvaluate(Staff s){
		return this.evaluateDaoImpl.findByStaff(s);
	}

    @Transactional(readOnly=true)
    public List<Evaluate> getEvaluate(Book book){
        return this.evaluateDaoImpl.findByBook(book);
    }

	@Transactional(readOnly=true)
	public List<Evaluate> getEvaluateOrderByPraiseDesc(Book book){
		return this.evaluateDaoImpl.findByBookOrderByPriaseDesc(book);
	}

	@Transactional(readOnly=true)
	public Page<Evaluate> getPageOrderByPraiseDesc(int pageNum,int pageSize,Book book){
		return this.evaluateDaoImpl.findByBookOrderByPriaseDesc(pageNum,pageSize,book);
	}

	@Transactional(readOnly=true)
	public Page<Evaluate> getAllPageOrderByPraiseForbidden(int pageNum,int pageSize){
		return this.evaluateDaoImpl.findAllOrderByForbiddenDesc(pageNum,pageSize);
	}

	@Transactional(readOnly=true)
	public List<Evaluate> getAllOrderByPraiseForbidden(int pageNum,int pageSize){
		return this.evaluateDaoImpl.findAllOrderByForbiddenDesc();
	}

	@Transactional(readOnly=true)
	public List<Evaluate> getEvaluate(Staff s, Book book){
		return this.evaluateDaoImpl.findByBookAndStaff(book,s);
	}

	public void updateEvaluate(Evaluate evaluate){
		this.evaluateDaoImpl.updateEvaluate(evaluate);
	}

    @Transactional(readOnly=true)
	/*查找全部评价记录*/
	public List<Evaluate> getAllEvaluate(Book book)
	{
		return this.evaluateDaoImpl.getAll(book);
	}

	/*删除指定评价记录*/
	public void deleteEvaluate(int evaluateId){
		Evaluate e = this.getEvaluateById(evaluateId);
		this.evaluateDaoImpl.deleteEvaluate(e);
    }

	/*删除多个评价记录*/
	public void deleteEvaluateGroup(int[] evaluates){
		Evaluate e = null;
		for(int i = 0;i < evaluates.length;++i){
			e = this.getEvaluateById(evaluates[i]);
			this.evaluateDaoImpl.deleteEvaluate(e);
		}
	}

    /*查找全部评论记录 按点赞数降序排序*/
    public List<Evaluate> getAllEvaluateOrderByPraiseDesc(Book book)
    {
        return this.evaluateDaoImpl.getAllOrderByPraiseDesc(book);
    }

	/*查找全部评论记录 按点赞数降序排序*/
	public List<Evaluate> getAllEvaluateOrderByPraiseDesc()
	{
		return this.evaluateDaoImpl.getAllOrderByPraiseDesc();
	}


	/*查找前三条评论记录 按点赞数降序排序*/
	public List<Evaluate> getTopThreeEvaluateOrderByPraiseDesc(Book book)
	{
		return this.evaluateDaoImpl.getTopThreeOrderByPraiseDesc(book);
	}

    /*查询数量*/
	@Transactional(readOnly=true)
	public Long getEvaluatesCountByParams(Map paramsMap){
		return this.evaluateDaoImpl.findCountByProperty(paramsMap);
	}

	/*查阅指定书籍的平均分*/
	public Float getBookAveGrade(Book book){
        List<Evaluate> evaluates = this.getEvaluate(book);
        float grade = 0;
		int count = 0;

        for(int i = 0;i < evaluates.size();++i)
			if(evaluates.get(i).getGrade() != null) {
				grade += evaluates.get(i).getGrade();
				++count;
			}
        if(count == 0)
        	grade = 0;
        else
        	grade /= count;

        return grade;
    }

}
